package com.dingreading.cloud.soms.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.dingreading.cloud.soms.dto.StoreSignInDto;
import com.dingreading.cloud.soms.entity.StoreSignIn;
import com.dingreading.cloud.soms.entity.table.StoreSignInTableDef;
import com.dingreading.cloud.soms.mapper.StoreSignInMapper;
import com.dingreading.cloud.soms.service.StoreSignInService;
import com.dingreading.cloud.soms.util.ScheduleUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;

import static com.dingreading.cloud.soms.entity.table.CmservicesTableDef.cmservices;
import static com.dingreading.cloud.soms.entity.table.MembersTableDef.members;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-09-11
 */
@Service
public class StoreSignInServiceImpl extends BaseServiceImpl<StoreSignInMapper, StoreSignIn, StoreSignInTableDef> implements StoreSignInService {

    @Override
    protected StoreSignInTableDef getTable() {
        return StoreSignInTableDef.storeSignIn;
    }

    @Resource
    private StoreSignInMapper storeSignInMapper;

    @Override
    public Page<StoreSignInDto> pageList(PageUtil pageUtil, Long storeId, String storeUid, String keyword, Integer status,
                                         String beginDate, String endDate, Integer readStatus) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (null != storeId)
            condition.and(table.storeId.eq(storeId));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.memberName.like(keyword));
        if (null != status)
            condition.and(table.status.eq(status));
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
            condition.and(table.arrivalTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));
        }

        if (null != readStatus) {
            QueryCondition serviceCondition = QueryCondition.createEmpty();
            serviceCondition.and(cmservices.storeUid.eq(storeUid));
            if (readStatus.equals(1))
                serviceCondition.and(cmservices.projectUid.eq(ScheduleUtil.HAODU_UID));
            else
                serviceCondition.and(cmservices.projectUid.ne(ScheduleUtil.HAODU_UID));

            QueryWrapper wrapper = QueryWrapper.create()
                    .select(cmservices.memberUid)
                    .from(cmservices)
                    .where(serviceCondition)
                    .groupBy(cmservices.memberUid);
            condition.and(table.memberUid.in(wrapper));
        }

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.storeName, table.memberUid, table.memberName, table.fullName, table.arrivalTime,
                        table.leaveTime, table.addTime, table.duration, table.signInStaffId, table.signOutStaffId,
                        table.status, table.signInType, table.signOutType, members.memberName.as("newMemberName")
                )
                .from(table)
                .leftJoin(members).on(table.memberUid.eq(members.uid))
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, StoreSignInDto.class);
    }

    @Override
    public JSONObject todayList(long storeId, String memberName, Integer status, String date) {
        List<JSONObject> list = storeSignInMapper.todayList(storeId, memberName, status, date);
        // 结果
        JSONObject jsonObject = new JSONObject();
        JSONArray arrivalList = new JSONArray();
        JSONArray leaveList = new JSONArray();
        if (CollectionUtils.isNotEmpty(list)) {
            for (JSONObject object : list) {
                Integer statusObj = object.getInteger("status");
                if (statusObj != null && statusObj.equals(1)) {
                    arrivalList.add(object);
                } else {
                    leaveList.add(object);
                }
            }
        }
        jsonObject.put("arrivalList", arrivalList);
        jsonObject.put("leaveList", leaveList);
        return jsonObject;
    }

    @Override
    public List<StoreSignIn> getStoreSignInList(Long storeId, String storeUid, String keyword, Integer status, String beginDate, String endDate, Integer readStatus) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (null != storeId)
            condition.and(table.storeId.eq(storeId));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.memberName.like(keyword));
        if (null != status)
            condition.and(table.status.eq(status));
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
            condition.and(table.arrivalTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));
        }

        if (null != readStatus) {
            QueryCondition serviceCondition = QueryCondition.createEmpty();
            serviceCondition.and(cmservices.storeUid.eq(storeUid))
                    .and(cmservices.lessonStatus.eq(0))
                    .and(cmservices.projectUid.eq("8067fd50183f4df4b808acbf34942590"));

            QueryWrapper wrapper = QueryWrapper.create()
                    .select(cmservices.memberUid)
                    .from(cmservices)
                    .where(condition)
                    .groupBy(cmservices.memberUid);

            condition.and(table.memberUid.in(wrapper));
        }

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.memberUid.asc(), table.arrivalTime.desc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public LinkedHashMap<String, Integer> getMemberSignInNum(Long storeId, List<String> memerUids, String beginDate, String endDate) {
        List<JSONObject> list = storeSignInMapper.getMemberSignInNum(storeId, memerUids, beginDate, endDate);
        // 结果
        LinkedHashMap<String, Integer> cntMap = new LinkedHashMap<>();
        if (CollectionUtils.isNotEmpty(list)) {
            for (JSONObject jsonObject : list) {
                String memberUid = jsonObject.getString("memberUid");
                Integer cnt = jsonObject.getInteger("cnt");
                cntMap.put(memberUid, null != cnt ? cnt : 0);
            }
        }
        return cntMap;
    }

    @Override
    public LinkedHashMap<String, Integer> getMemberSignInCnt(List<String> memerUids) {
        List<JSONObject> list = storeSignInMapper.getMemberSignInCnt(memerUids);
        // 结果
        LinkedHashMap<String, Integer> cntMap = new LinkedHashMap<>();
        if (CollectionUtils.isNotEmpty(list)) {
            for (JSONObject jsonObject : list) {
                String memberUid = jsonObject.getString("memberUid");
                Integer cnt = jsonObject.getInteger("cnt");
                cntMap.put(memberUid, null != cnt ? cnt : 0);
            }
        }
        return cntMap;
    }

}
